package com.parking.status.sync.netty.server;

import com.parking.status.sync.netty.entity.MessageBody;
import com.parking.status.sync.netty.entity.MessageType;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * Created by Administrator on 2015/9/15.
 */
public class BodyFrameDecoder extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> out) throws Exception {
        MessageBody body = new MessageBody();

        short companyCode = byteBuf.readShort();
        body.setCompanyCode(companyCode);

        short addressCode = byteBuf.readShort();
        body.setAddrCode(addressCode);

        int revCode = byteBuf.readInt();
        body.setReceiverAddress(revCode);

        byte control = byteBuf.readByte();
        body.setControlByte(control);

        ByteBuf bufContent = byteBuf.readBytes(body.getContentLength());
        body.setContent(bufContent.array());

        ByteBuf bufCrc = byteBuf.readBytes(2);
        body.setCrc16(bufCrc.array());

        byte endian = byteBuf.readByte();
        body.setEndian(endian);

        out.add(body);
    }
}
